package view
{
    import model.*;
    import mx.resources.IResourceManager;
    import mx.resources.ResourceManager;

    import org.kolonitsky.alexey.algorithm.UintBitset;
    import org.kolonitsky.alexey.mvcext.NotificationMap;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import view.AppPreloader;

    [ResourceBundle("common")]

    public class PreloaderMediator extends Mediator
    {
        public static const NAME:String = "preloaderMediator";

        public static const CONNECTION_ESTABLISHED:uint = 1 << 1;
        public static const USER_AUTHORIZED:uint = 1 << 2;
        public static const APPLICATION_READY:uint = 1 << 3;

        public function PreloaderMediator(preloader:AppPreloader)
        {
            super(NAME, preloader);
        }


        public function get preloader():AppPreloader
        {
            return viewComponent as AppPreloader
        }




        //-------------------------------------------------------------------------
        //
        //  Override mediator
        //
        //-------------------------------------------------------------------------

        override public function listNotificationInterests():Array
        {
            return [
                NotificationMap.PAGE_READY,
                ElfinProxy.CONNECTION_ESTABLISHED,
                ElfinProxy.CONNECTION_LOOSE,
                UserProxy.USER_AUTHORIZATION
            ];
        }

        override public function handleNotification(notification:INotification):void
        {
            super.handleNotification(notification);
            switch (notification.getName())
            {
                case ElfinProxy.CONNECTION_ESTABLISHED:
                {
                    updateStatus(CONNECTION_ESTABLISHED);
                    break;
                }
                case ElfinProxy.CONNECTION_LOOSE:
                {
                    var rm:IResourceManager = ResourceManager.getInstance();
                    preloader.setPreloaderString(rm.getString("common", "connectionLost"));
                    break;
                }
                case UserProxy.USER_AUTHORIZATION:
                {
                    updateStatus(USER_AUTHORIZED);
                    break;
                }
                case NotificationMap.PAGE_READY:
                {
                    updateStatus(APPLICATION_READY);
                    break;
                }
            }
        }




        //-------------------------------------------------------------------------
        //
        //  Override mediator
        //
        //-------------------------------------------------------------------------

        private var status:UintBitset = new UintBitset(0);
        
        private function updateStatus(flag:uint):void
        {
            if (status.test(flag))
                return;
            
            status.set(flag);
            preloader.completeCallback(flag);
        }
    }
}
